'''
Created on Oct 25, 2012

@author: peng
'''

def need_split():
    if str(datetime.datetime.now().hour) in ['23', '0', '00']:
        return True
    return False


def split_file(file_name):
    today = datetime.datetime.now().strftime('%Y%m%d')
    yesterday = (datetime.datetime.now() - datetime.timedelta(days=1))
    yesterday = yesterday.strftime('%Y%m%d')

    f1_name = file_name + "-split-1"
    f2_name = file_name + "-split-2"

    f1 = open(f1_name, 'w')
    f2 = open(f2_name, 'w')
    f_dict = {today: f1, yesterday: f2}

    f = open(file_name)
    for l in f.readlines():
        values = l.split('\t')
        if len(values) < 25:
            continue
        t = values[24]
        try:
            t = int(t) / 1000
        except Exception, ex:
            _logger.error("[split file][%s]" % str(ex))
            continue
        t = datetime.datetime.fromtimestamp(t).strftime('%Y%m%d')
        if t in f_dict:
            f = f_dict[t]
            f.write(l + '\n')

    f.close()
    f1.close()
    f2.close()
    return {today: f1_name, yesterday: f2_name}

if __name__ == '__main__':
    pass
